﻿package net.wangit.framework.config;

import net.wangit.Path;
import net.wangit.user.SuperUserOption;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;


/**
 * 应用设置
 * 
 * @author watson
 * 
 */
public class ProjectSettings {
	private final Log log = LogFactory.getLog(ProjectSettings.class);
	private static ProjectSettings project;
	public static final String FILE_NAME = "project.xml";

	private int logBufferSize; // 日志队列大小
	private int superUserOption; // 超级用户状态选项
	private boolean trackEnabled;// 启用跟踪器
	private boolean recordEnabled;// 启用计时器
	private boolean reloadI18n;// 每次装载语言文件
	private boolean reloadTemplate;// 每次装载模板文件

	private ProjectSettings() {
	}

	public static ProjectSettings instance() {
		if (project == null) {
			project = new ProjectSettings();
			project.loadDefault();
		}
		return project;
	}

	private void loadDefault() {
		logBufferSize = 100;
		superUserOption = SuperUserOption.ENABLED;
		trackEnabled = false;
		recordEnabled = false;
		reloadI18n = false;
		reloadTemplate = false;
	}

	public void load() {
		String path = Path.getConfigPath(FILE_NAME);
		if (FileUtil.exists(path)) {
			Document document = XMLUtil.parseByPath(path);
			Element root = document.getRootElement();
			superUserOption = XMLUtil.getTextInt(root, "superUserOption",
					SuperUserOption.ENABLED);
			logBufferSize = XMLUtil.getTextInt(root, "logBufferSize", 100);
			recordEnabled = XMLUtil
					.getTextBoolean(root, "recordEnabled", false);
			trackEnabled = XMLUtil.getTextBoolean(root, "trackEnabled", false);
			reloadI18n = XMLUtil.getTextBoolean(root, "reloadI18n", false);
			reloadTemplate = XMLUtil.getTextBoolean(root, "reloadTemplate",
					false);
			log.info("load project settings successfully.");
		}else{
			this.loadDefault();
		}
	}

	public int getLogBufferSize() {
		return logBufferSize;
	}

	public int getSuperUserOption() {
		return superUserOption;
	}

	public boolean isReloadI18n() {
		return reloadI18n;
	}

	public boolean isReloadTemplate() {
		return reloadTemplate;
	}

	public boolean isTrackEnabled() {
		return trackEnabled;
	}

	public boolean isRecordEnabled(String domain) {
		return recordEnabled && !Domains.isSystemDomain(domain);
	}

}
